package com.example.controller;


import com.example.entity.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class HelloController {
    @RequestMapping("/")
    public String hello(Person person) {
        return "Hello,world person:" + person;
    }

    @RequestMapping("/m1")
    public String m1(@RequestParam(value = "name", required = false) String str) {
        return "Hello,world name:" + str;
    }

    @RequestMapping("/m2")
    public String m2(String[] str) {
        return "Hello,world str:" + Arrays.toString(str);
    }

    @RequestMapping("/m3")
    public String m3(@RequestParam List<String> str) {
        return "Hello,world str:" + str;
    }

    @RequestMapping("/m4")
    public String m4(@RequestBody Person person) {
        return "Hello,world person:" + person;
    }

    @RequestMapping("/m5/{name}/{id}")
    public String m5(@PathVariable Integer id, @PathVariable("name") String username) {
        return "Hello,world person:" + id + username;
    }

    @RequestMapping("/m6")
    public String m6(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("C:\\Users\\HP\\Desktop\\" + fileName));
        return "接收文件名称:" + fileName;
    }

    @RequestMapping("/m7")
    public String m7(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "Cookie:" + builder;
    }

    @RequestMapping("/m8")
    public String m8(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        StringBuilder builder = new StringBuilder();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                builder.append(ck.getName() + ":" + ck.getValue());
            }
        }
        return "Cookie:" + builder;
    }

    @RequestMapping("/m9")
    public String m9(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("test", "hello");
        }
        return "session set";
    }

    @RequestMapping("/m10")
    public String m10(HttpSession session) {
        return "session:" + session.getAttribute("test");
    }

    @RequestMapping("/m11")
    public String m11(@SessionAttribute(value = "username", required = false) String name) {//获取指定key
        return "session:" + name;
    }


}
